﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.Reflection;
using System.Diagnostics;

namespace MyCollection
{
    public partial class ProductConfig : Form
    {
        private string htmlTemplet = File.Exists(CollectSetting.Default.HtmlTemplet) ? 
            File.ReadAllText(CollectSetting.Default.HtmlTemplet) : String.Empty;

        public ProductConfig()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (currentRow != null)
            {
                tbListUrl.Text = currentRow.CollectUrl;
                tbListStart.Text = currentRow.ListStartString;
                tbListOver.Text = currentRow.ListOverString;
                tbHrefStart.Text = currentRow.HerfStartString;
                tbHrefOver.Text = currentRow.HerfOverString;
                tbTitleStart.Text = currentRow.TitleStartString;
                tbTitleOver.Text = currentRow.TitleOverString;
                tbImgStart.Text = currentRow.ImgStartString;
                tbImgOver.Text = currentRow.ImgOverString;
                tbContStart.Text = currentRow.ContStartString;
                tbContOver.Text = currentRow.ContOverString;
                tbPriceStart.Text = currentRow.PriceStartString;
                tbPriceOver.Text = currentRow.PriceOverString;
                textBox1.Text = currentRow.CollectName;
                checkBox1.Checked = currentRow.IsUtf;
            }

        }

        private MyCollectDataSet.CollectProgramRow currentRow;

        /// <summary>
        /// 当前选择项目行
        /// </summary>
        public MyCollectDataSet.CollectProgramRow CurrentRow
        {
            get { return currentRow; }
            set { currentRow = value; }
        }

        private MyCollectDataSet.CollectProgramRow changeRow;

        /// <summary>
        /// 改变后的项目行
        /// </summary>
        public MyCollectDataSet.CollectProgramRow ChangeRow
        {
            get { return changeRow; }
            set { changeRow = value; }
        }




        private void button1_Click(object sender, EventArgs e)
        {

            if (currentRow != null)
            {
                currentRow.CollectUrl = tbListUrl.Text;
                currentRow.ListStartString = tbListStart.Text;
                currentRow.ListOverString = tbListOver.Text;
                currentRow.HerfStartString = tbHrefStart.Text;
                currentRow.HerfOverString = tbHrefOver.Text;
                currentRow.TitleStartString = tbTitleStart.Text;
                currentRow.TitleOverString = tbTitleOver.Text;
                currentRow.ContStartString = tbContStart.Text;
                currentRow.ContOverString = tbContOver.Text;
                currentRow.ImgStartString = tbImgStart.Text;
                currentRow.ImgOverString = tbImgOver.Text;
                currentRow.PriceStartString = tbPriceStart.Text;
                currentRow.PriceOverString = tbPriceOver.Text;
                currentRow.IsUtf = checkBox1.Checked;
                currentRow.CollectName = textBox1.Text;
            }
            else
            {

                CollectForm parentForm = (CollectForm)this.Owner;
                Collect cl = new Collect();
                cl.CollectUrl = tbListUrl.Text;
                cl.ListStart = tbListStart.Text;
                cl.ListOver = tbListOver.Text;
                cl.HrefStart = tbHrefStart.Text;
                cl.HrefOver = tbHrefOver.Text;
                cl.TitleStart = tbTitleStart.Text;
                cl.TitleOver = tbTitleOver.Text;
                cl.ConStart = tbContStart.Text;
                cl.ConOver = tbContOver.Text;
                cl.ImgStart = tbImgStart.Text;
                cl.ImgOver = tbImgOver.Text;
                cl.PriceStart = tbPriceStart.Text;
                cl.PriceOver = tbPriceOver.Text;
                cl.IsUTF = checkBox1.Checked;
                cl.CollectName = textBox1.Text;

                parentForm.Collect = cl;

            }
           


            this.Close();


            //RemoteHtml remote = new RemoteHtml(cl);
            ////File.Copy(CollectSetting.Default["GoodsTemplet"].ToString(), @"D:\办公\香水彩妆\宝贝数据库\222.csv", true);
            //using (StreamWriter sw = File.CreateText(@"D:\办公\香水彩妆\宝贝数据库\" + textBox1.Text + ".csv"))
            //{
            //    sw.WriteLine();
            //    foreach (string s in remote.HrefCollection())
            //    {
            //        Commodity comm = new Commodity(cl, CollectClass.DefiniteUrl(s, cl.CollectUrl));
            //        string contents = htmlTemplet.Replace("{$商品描述}", comm.Content);
            //        contents = (contents.Length > 25000 ? comm.Content : contents);
            //        contents = CollectClass.GetStringByNoSpace(contents);
            //        string content = "";
            //        content = content + "\"" + comm.Title + "\"\t";
            //        content = content + "50010810\t44251086\t5\t\"上海\"\t\"上海\"\t\"b\"\t";
            //        content = content + comm.Price.ToString() + "\t";
            //        content = content + "0.000000\t1000\t7\t1\t10.000000\t20.000000\t5.000000\t0\t1\t0\t0\t1\t0\t0\t";
            //        content = content + "\"1980-1-1  0:00:00\"\t\"\"\t";
            //        content = content + "\"" + contents + "\"\t";
            //        content = content + "\"" + comm.ImgPath + "\"\t";
            //        content = content + "\"34580:27133;34581:20165;34582:10122;30964:96241;\"\t";
            //        content = content + "0.000000\t0\t0\t0\t\"2008-07-11 13:56:37\"\t100\t0\t";
            //        sw.WriteLine(content);
            //    }
            //    sw.Close();
            //}
            
            //MessageBox.Show("采集完毕");

            #region excel注释掉
            //string templetFilename = @"D:\办公\香水彩妆\宝贝数据库\宝贝模板.xls";
            //Microsoft.Office.Interop.Excel.Application objApp = new Microsoft.Office.Interop.Excel.Application();
            //Microsoft.Office.Interop.Excel.Workbooks workbooks = objApp.Workbooks;
            //object missing = Missing.Value;
            //workbooks.Open(templetFilename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
            //Microsoft.Office.Interop.Excel.Sheets objSheets = objApp.Worksheets;
            //Microsoft.Office.Interop.Excel._Worksheet objSheet;
            //objSheet = (Microsoft.Office.Interop.Excel._Worksheet)objSheets.get_Item(1);

            //写入从第二行 行首开始
            //int row = 2;

            //foreach (string s in remote.HrefCollection())
            //{
            //    Commodity comm = new Commodity(cl, CollectClass.DefiniteUrl(s, cl.CollectUrl));
            //    for (int i = 1; i <= 34; i++)
            //    {
            //        switch (i)
            //        {
            //            case 1:
            //                objSheet.Cells[row, i] = comm.Title;
            //                break;
            //            case 8:
            //                objSheet.Cells[row, i] = comm.Price;
            //                break;
            //            case 25:
            //                objSheet.Cells[row, i] = comm.Content;
            //                break;
            //            case 26:
            //                objSheet.Cells[row, i] = comm.ImgPath;
            //                break;
            //            default:
            //                objSheet.Cells[row, i] = objSheet.Cells[2, i];
            //                break;
            //        }

            //    }
            //    row = row + 1;
            //}

            //保存格式
            //string FileName = objApp.GetSaveAsFilename(CollectSetting.Default["SaveCSVPath"], "CSV文件 (*.csv), *.csv", 1, "Hello", "Hi").ToString().Trim();
            //if (FileName != "False" && FileName != "")
            //{
            //    objSheet.SaveAs(FileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlCSV, "", "", false, false, false, 1, 1, true);//保存的类型，可设置密码 只读 ..
            //}

            //objApp.Workbooks.Close();
            ////退出依然会驻留进程,因此进行垃圾回收  
            //objApp.Quit();
            //System.GC.Collect();
            #endregion

        }



    }
}